﻿using System;
using System.Windows;
using System.Windows.Controls;
using Tridion.ContentManager.CoreService.Client;
using TridionDesktopTools.Core;

namespace TridionDesktopTools.AdvancedSearch
{
    public class CreteriaRow
    {
        public ComboBox cbField { get; set; }
        public TextBlock txtType { get; set; }
        public ComboBox cbOperation { get; set; }
        public TextBox txtValue { get; set; }
        public DatePicker dateValue { get; set; }
        public ComboBox cbValue { get; set; }
        public Button btnRemove { get; set; }

        public int Index { get; set; }

        public ItemFieldDefinitionData Field
        {
            get
            {
                return this.cbField.SelectedValue as ItemFieldDefinitionData;
            }
            set
            {
                ItemFieldDefinitionData field = value;

                if (field.IsDate())
                {
                    this.txtValue.Visibility = Visibility.Hidden;
                    this.dateValue.Visibility = Visibility.Visible;
                    this.cbValue.Visibility = Visibility.Hidden;
                }
                else if (field.IsKeyword() || field.IsTextSelect())
                {
                    this.txtValue.Visibility = Visibility.Hidden;
                    this.dateValue.Visibility = Visibility.Hidden;
                    this.cbValue.Visibility = Visibility.Visible;
                }
                else
                {
                    this.txtValue.Visibility = Visibility.Visible;
                    this.dateValue.Visibility = Visibility.Hidden;
                    this.cbValue.Visibility = Visibility.Hidden;
                }

                if (this.cbField != null && field != null)
                    this.cbField.SelectedValue = field;
            }
        }

        public object Value
        {
            get
            {
                if (this.txtValue != null && this.txtValue.Visibility == Visibility.Visible)
                {
                    if (this.Field.IsNumber())
                    {
                        try
                        {
                            return double.Parse(this.txtValue.Text);
                        }
                        catch (Exception)
                        {
                            return null;
                        }
                    }
                    return this.txtValue.Text;
                }
                if (this.dateValue != null && this.dateValue.Visibility == Visibility.Visible)
                {
                    return this.dateValue.SelectedDate;
                }
                if (this.cbValue != null && this.cbValue.Visibility == Visibility.Visible)
                {
                    if (this.Field.IsKeyword())
                    {
                        ItemInfo item = this.cbValue.SelectedValue as ItemInfo;
                        return item == null ? null : item.Title;
                    }
                    if (this.Field.IsTextSelect())
                    {
                        return this.cbValue.SelectedValue.ToString();
                    }
                }
                return null;
            }
            set
            {
                this.txtValue.Text = value.ToString();
                try
                {
                    this.dateValue.SelectedDate = value as DateTime?;
                }
                catch (Exception)
                {
                    this.dateValue.SelectedDate = null;
                }
                try
                {
                    this.cbValue.Text = value.ToString();
                }
                catch (Exception)
                {
                    this.cbValue.SelectedValue = null;
                }
            }
        }
    }
}
